#!/bin/bash
#
# recover-mysql-master 脚本 2016-08-29
#
test -z "$1" && \
	echo 'ERROR! recover-mysql.sh必要参数不能为空！参数要求: 学校code（大写）' \
    && exit -1
schoolCode=$1
schoolCode=`echo $schoolCode | tr '[A-Z]' '[a-z]'`
schoolCodeUpper=`echo $schoolCode | tr '[a-z]' '[A-Z]'`
echo "schoolCode is $schoolCode"
masterHostIp=`kubectl get pod -owide | grep $schoolCode-mysql | grep -v $schoolCode-mysql-s|awk '{print $6}'`
slaveHostIp=`kubectl get pod -owide | grep $schoolCode-mysql-s|awk '{print $6}'`
test -z "$masterHostIp" && \
	echo "Error! masterHostIp is null." && \
	exit -1
test -z "$slaveHostIp" && \
	echo "Error! slaveHostIp is null." && \
	exit -1
echo "master on $masterHostIp, slave on $slaveHostIp."

rcName=$schoolCode-mysql

kubectl scale rc $rcName --replicas=0

test $? -ne 0 && echo "Error! stop mysql master Error." && exit -1

ssh root@$masterHostIp "rm -rf /var2/mysql/$schoolCodeUpper/*"
